#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
require 'find'

##----------2222-----111---------------------------修改spec文件-----------------------------------------------------------##

class TNFT

    path = Pathname.new(__FILE__).realpath

    PROJECT_DIR = File.dirname(path)
    PROJECT_NAME = File.basename(PROJECT_DIR)



    specPath = "#{PROJECT_DIR}/#{PROJECT_NAME}.podspec"

    PROJECT_VERSION = ''

    def modifySpec
        puts "-----------------------------修改spec文件，-------------------------\n添加  if ENV[s.name+'_use_code']"
        specFile = File.new("#{PROJECT_NAME}.podspec","r+")

        if specFile

            lineIdx=0
            content = ""
            specFile.each_line do |line|
                tmpLine = line
                tmpLine = tmpLine.gsub(' ','')
                if tmpLine.include?("s.version=")

                    tmpLine = tmpLine.gsub('"','')
                    tmpLine = tmpLine.gsub('\'','')
                    tmpLine = tmpLine.gsub('s.version=','')

                    PROJECT_VERSION.replace tmpLine.strip
                end
                content+=line
            end
            File.open("#{PROJECT_NAME}.podspec","w"){|l| #以写的方式打开文件，将buffer覆盖写入文件
              l.write(content)
            }
        else
          puts ">>ERROR:没找到spec文件！！"
        end
    end
    ###------------------------------------------清理pod缓存----------------------------------------------------------##
    def cleanProjectCache
        exampleDir = "#{PROJECT_DIR}/Example"
        podsDir = "#{exampleDir}/Pods"

        puts "开始清理依赖库文件 ，#{podsDir}"
        FileUtils.rm_r("#{exampleDir}/#{PROJECT_NAME}.xcworkspace")
        FileUtils.rm("#{exampleDir}/Podfile.lock")
        FileUtils.rm_r "#{podsDir}"
        puts "清理完毕"
    end
    def cleanPodCache

        exampleDir = "#{PROJECT_DIR}/Example"
        podsDir = "#{exampleDir}/Pods"
        isDir = File.directory?(podsDir)
        if isDir

            puts "-----------------------------清理pod缓存-----------------------------"

            Dir.foreach(podsDir) do |filename|
                    if filename != "." &&
                        filename != ".." &&
                        filename != "Local Podspecs" &&
                        filename != "Target Support Files" &&
                        File.directory?("#{podsDir}/#{filename}") &&
                        filename != "Pods.xcodeproj" &&
                        filename != "Headers"

                        syswinRepoItem = "#{File.expand_path('~')}/.cocoapods/repos/MMSpecs/#{filename}"

                        if File.directory?(syswinRepoItem)
                            puts "删除pod缓存 > #{filename}"

                            system "pod cache clean #{filename} --all"
                        else
                            puts "-----------跳过 #{filename} "
                        end
                    end
            end

        else
            puts "-----------------------------pods目录不存在，#{podsDir}-----------------------------"
        end
    end
    ##------------------------------------------打包Framework----------------------------------------------------------##
    def buildFramework

        puts "-----------------------------打包Framework-----------------------------"
        frmPath = "#{PROJECT_DIR}/Frameworks"
        FileUtils.mkdir_p(frmPath) unless File.exists?(frmPath)

        system "#{PROJECT_NAME}_use_code=1 pod framework #{PROJECT_NAME}.podspec --no-force  --spec-sources=https://gitee.com/chengssir/MMSpecs.git,https://github.com/CocoaPods/Specs.git"

       newFrmFolder = "#{PROJECT_DIR}/#{PROJECT_NAME}-#{PROJECT_VERSION}"
       puts "新包地址 -----> " + newFrmFolder
       newFrmPath =  "#{newFrmFolder}/#{PROJECT_NAME}.xcframework"
       puts "新包绝对路径 -----> " + newFrmPath
       if File.exists?(newFrmPath)
            puts "-----------------------------迁移文件-----------------------------"
            originFrmPath = "#{frmPath}/#{PROJECT_NAME}.xcframework"
            puts "originFrmPath地址 #{originFrmPath}"

            if File.exists?(originFrmPath)
                   FileUtils.rm_r(originFrmPath)
                   puts '删除原有Framework ' + originFrmPath
            end
             puts "拷贝Framework到目标路径Frameworks" + newFrmPath
             FileUtils.cp_r(newFrmPath, originFrmPath)
#             puts "清理临时文件Framework"
#             FileUtils.rm_r(newFrmFolder)
      else
             puts "打包Framework失败"
       end


    end

    def installFramework
        puts "-------------------------安装Framework----------------------------------"
        FileUtils.cd("Example")
  #      FileUtils.cd("../")
        system "pod install"
        system "open #{PROJECT_NAME}.xcworkspace"
    end

end

TNFT.new.modifySpec
p "项目名称> " +TNFT::PROJECT_NAME
p "项目版本> " +TNFT::PROJECT_VERSION
p "项目路径> " +TNFT::PROJECT_DIR

TNFT.new.cleanPodCache
TNFT.new.buildFramework

frmPath = "#{TNFT::PROJECT_DIR}/Frameworks/#{TNFT::PROJECT_NAME}.xcframework"
puts "新包绝对路径： -----> " + frmPath

 if File.exists?(frmPath)
    TNFT.new.cleanProjectCache
    TNFT.new.installFramework

    puts ">>> 请编译项目成功后提交代码，打tag，推送pod库 <<<"

 else
    puts ">>> 请检查项目错误，重新执行脚本 <<< "
 end




